package cn.humaohua.study.mongotransaction.service;

import cn.humaohua.study.mongotransaction.Test;
import cn.humaohua.study.mongotransaction.repo.TestRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TestServiceImpl implements TestService {
    @Autowired
    TestRepository testRepository;

    @Transactional
    @Override
    public Object t1(Boolean rollback) {
        long i1 = testRepository.count();
        Test t1 = new Test("test1", "success");
        testRepository.insert(t1);
        Test t2 = new Test("test2", "fail");
        if (rollback) {
            throw new RuntimeException("test transaction rollback");
        }
        testRepository.insert(t2);
        long i2 = testRepository.count();
        return "数据插入正常,之前有"+i1+" 条数据,现在有"+i2+" 条数据";
    }
}
